package com.deng.controller;

import com.deng.dao.DepartmentDao;
import com.deng.dao.EmployeeDao;
import com.deng.mapper.EmployeeMapper;
import com.deng.pojo.Department;
import com.deng.pojo.Employee;
import com.deng.service.DepartmentService;
import com.deng.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @create 2020-12-14 14:34
 */
@Controller
public class EmployeeController {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;

    @RequestMapping("/emps")
    public String list(Model model) {
//        Collection<Employee> employees = employeeDao.getAllEmployees();
        List<Employee> employees = employeeService.getAllEmployees();
        model.addAttribute("emps", employees);
        return "emp/list";//返回到list页面
    }

    @RequestMapping("/dash")
    public String dash(Model model) {
        return "dashboard";
    }

    @GetMapping("/add")
    public String add(Model model) {
        //查出所有的部门信息,添加到departments中,用于前端接收
//        Collection<Department> departments = departmentDao.departments();
        List<Department> departments = departmentService.getAllDepartments();
        model.addAttribute("departments", departments);
        return "emp/add";//返回到添加员工页面
    }

    @PostMapping("/add")
    public String addEmp(Employee employee) {
//        employeeDao.addEmployee(employee);//添加一个员工
        Department departmentById = departmentService.getDepartmentById(employee.getDepartment().getId());
        employee.setDepartment(departmentById);
        employeeService.addEmployee(employee);
        return "redirect:/emps";//重定向到/emps,刷新列表,返回到list页面
    }

    //restful风格接收参数
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") int id, Model model) {
        //查询指定id的员工,添加到empByID中,用于前端接收
//        Employee employeeByID = employeeDao.getEmployeeByID(id);
        Employee employeeById = employeeService.getEmployeeById(id);
        model.addAttribute("empByID", employeeById);
        //查出所有的部门信息,添加到departments中,用于前端接收
        List<Department> departments = departmentService.getAllDepartments();
        model.addAttribute("departments", departments);
        return "/emp/edit";//返回到编辑员工页面
    }

    @PostMapping("/edit")
    public String EditEmp(Employee employee) {
//        employeeDao.addEmployee(employee);//添加一个员工
        Department departmentById = departmentService.getDepartmentById(employee.getDepartment().getId());
        employee.setDepartment(departmentById);
        employeeService.updateEmployee(employee);
//        employeeService.addEmployee(employee);
        return "redirect:/emps";//添加完成重定向到/emps,刷新列表
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id) {
//        employeeDao.deleteEmployeeByID(id);
        employeeService.deleteEmployeeById(id);
        return "redirect:/emps";
    }

    @RequestMapping("/user/logout")
    public String logout(HttpSession session) {
//        session.invalidate();
        session.removeAttribute("LoginUser");
        return "redirect:/index.html";
    }


}
